package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.CateBO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.CateDTO;
import com.lwc.cfdns.pojo.entity.SystemCate;
import com.lwc.cfdns.pojo.vo.system.CateTreeVO;
import com.lwc.cfdns.pojo.vo.system.CateVO;
import com.lwc.cfdns.service.impl.SystemCateServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 通用分类控制器
 *
 * @author luwc
 * @title CateContorller
 * @description
 * @time 2023/5/30 22:32
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class CateContorller {
    @Autowired
    private SystemCateServiceImpl systemCateService;

    /**
     * 创建
     *
     * @param cateDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:35
     */
    @PreAuthorize("hasAnyAuthority('system:cate:create')")
    @PostMapping("/cate")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody CateDTO cateDTO) {
        return systemCateService.create(cateDTO);
    }

    /**
     * 修改
     *
     * @param cateDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:44
     */
    @PreAuthorize("hasAnyAuthority('system:cate:update')")
    @PutMapping("/cate")
    public Map<String, Object> update(@Validated(ValidGroup.Create.class) @RequestBody CateDTO cateDTO) {
        return systemCateService.update(cateDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return CateVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:44
     */
    @PreAuthorize("hasAnyAuthority('system:cate:view')")
    @GetMapping("/cate/{id}")
    public CateVO view(@PathVariable("id") @NotNull(message = "分类ID不能为空") Long id) {
        return systemCateService.view(id);
    }

    /**
     * 删除(可批量删除)
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:44
     */
    @PreAuthorize("hasAnyAuthority('system:cate:delete')")
    @DeleteMapping("/cate/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotBlank(message = "分类ids不能为空") String ids) {
        return systemCateService.delete(ids);
    }

    /**
     * 列表
     *
     * @param cateBO
     * @return IPage<SystemCate>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 22:45
     */
    @PreAuthorize("hasAnyAuthority('system:cate:lists')")
    @GetMapping("/cate")
    public IPage<SystemCate> lists(CateBO cateBO) {
        return systemCateService.lists(cateBO);
    }

    /**
     * 树
     *
     * @param cateBO
     * @return List<CateVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 23:20
     */
    @GetMapping("/cate/tree")
    public List<CateTreeVO> tree(CateBO cateBO) {
        cateBO.setStatus("0");
        return systemCateService.tree(cateBO);
    }
}
